package com.qdxwx.manage.controller.help;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.entity.Ztree;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.help.HelpService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.help.Help;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 帮助菜单Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/help")
public class HelpController extends BaseController {

    @Autowired
    private HelpService helpService;

    @GetMapping("/list.html")
    public String list() {
        return "help/help-list";
    }

    @PostMapping("/list.json")
    @ResponseBody
    public List<Help> list(HttpServletRequest request) {
        Help help = new Help();
        help.setName(request.getParameter("name"));
        return helpService.getList(help);
    }

    @GetMapping("/add.html")
    public String add() {
        return "help/help-add";
    }

    /**
     * 新增保存帮助菜单
     */
    @Log(title = "添加帮助菜单", businessType = BusinessType.INSERT)
    @Permissions("help:edit")
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest request) {
        String name = request.getParameter("name");
        if (TKString.isEmpty(name)) return error("标题不能为空");
        Help help = new Help();
        help.setParentId(TKConvert.toLong(request.getParameter("parentId")));
        help.setName(name);
        help.setLinkBuilding(request.getParameter("linkBuilding"));
        help.setContent(request.getParameter("content"));
        help.setSort(TKConvert.toLong(request.getParameter("sort")));
        return toAjax(helpService.insert(help));
    }

    /**
     * 修改帮助菜单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") long id, ModelMap mmap) {
        if (id < 1) return error404;
        Help help = helpService.get(id);
        if (help == null) return error404;
        mmap.put("helpMenu", help);
        return "help/help-edit";
    }

    /**
     * 修改保存帮助菜单
     */
    @Log(title = "编辑帮助菜单", businessType = BusinessType.UPDATE)
    @Permissions("help:edit")
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(HttpServletRequest request) {
        long id = TKConvert.toLong(request.getParameter("id"));
        if (id < 1) return error("缺少参数id");
        Help help = helpService.get(id);
        if (help == null) return error("找不到指定数据");
        String name = request.getParameter("name");
        if (TKString.isEmpty(name)) return error("标题不能为空");
        help.setName(name);
        help.setLinkBuilding(request.getParameter("linkBuilding"));
        help.setContent(request.getParameter("content"));
        help.setSort(TKConvert.toLong(request.getParameter("sort")));
        return toAjax(helpService.update(help));
    }

    /**
     * 删除
     */
    @Log(title = "删除帮助菜单", businessType = BusinessType.DELETE)
    @Permissions("help:remove")
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") long id) {
        if (id < 1) return error("缺少id参数");
        List<Help> list = helpService.getByParentId(id);
        if (list.size() > 0) {
            return error("该菜单已分配有下级，不能删除。");
        }
        return toAjax(helpService.delete(id));
    }

    /**
     * 选择帮助菜单树
     */
    @GetMapping(value = {"/tree/{id}", "/tree/"})
    public String selectHelpmenuTree(@PathVariable(value = "id", required = false) long id, ModelMap mmap) {
        if (id > 0) {
            mmap.put("helpMenu", helpService.get(id));
        }
        return "help/help-tree";
    }

    /**
     * 加载帮助菜单树列表
     */
    @GetMapping("/tree.json")
    @ResponseBody
    public List<Ztree> treeData() {
        return helpService.getTree();
    }
}
